package com.base.cn.platform.os.service.qa.record;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.BaseDao;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.qa.question.QaQuestion;
import com.base.cn.platform.os.entity.qa.question.QuestionCondition;
import com.base.cn.platform.os.entity.qa.record.QaGainAttendRecord;
import com.base.cn.platform.os.entity.qa.record.QaRecordBaseEntity;
import com.base.cn.platform.os.service.qa.question.QaQuestionBiz;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LIZHIPENG
 * @since 2018/5/4
 */
public class QaRecordBaseBiz<T extends QaRecordBaseEntity, K extends BaseDao<T>> extends BaseBiz<T, K>{

    @Autowired
    private QaQuestionBiz qaQuestionBiz;


    public String getWhereSql(T t){
        StringBuilder whereSql = new StringBuilder("1=1 and status !=3");
        if(ObjectUtils.isNotEmpty(t)){
            if(DataUtil.idIsNotNull(t.getId())){
                whereSql.append(" and id=").append(t.getId());
            }
            if(StringUtils.isNotEmpty(t.getOrderNo())){
                whereSql.append(" and orderNo like '%"+ t.getOrderNo().trim()).append("%'");
            }
            if(DataUtil.idIsNotNull(t.getTeacherId())){
                whereSql.append(" and teacherId = ").append(t.getTeacherId());
            }
            if(StringUtils.isNotEmpty(t.getQuestionContext())){
                QuestionCondition condition = new QuestionCondition();
                condition.setContext(t.getQuestionContext());
                List<QaQuestion> questionList = qaQuestionBiz.findQuestionList(condition, false, false, false, false, false, false, false);
                if(ObjectUtils.isNotEmpty(questionList)){
                    String questionIds = questionList.stream().map(e -> e.getId().toString()).distinct().collect(Collectors.joining(","));
                    whereSql.append(" and questionId in (").append(questionIds).append(")");
                }else{
                    whereSql.append(" and questionId in (0)");
                }
            }
            if(StringUtils.isNotEmpty(t.getQuestionIds()) && StringUtils.isNotEmpty(t.getQuestionIds().trim())){
                whereSql.append(" and questionId in (").append(t.getQuestionIds() ).append(")");
            }
            if(StringUtils.isNotEmpty(t.getQuestionIds()) && StringUtils.isNotEmpty(t.getQuestionIds().trim())){
                whereSql.append(" and questionId in (").append(t.getQuestionIds() ).append(")");
            }
            if(StringUtils.isNotEmpty(t.getBeginTime())){
                whereSql.append(" and createTime>='").append(t.getBeginTime()).append("'");
            }
            if(StringUtils.isNotEmpty(t.getEndTime())){
                whereSql.append(" and createTime<='").append(t.getEndTime()).append("'");
            }
            if(ObjectUtils.isNotEmpty(t.getStatus()) && t.getStatus()!=0){
                whereSql.append(" and status=").append(t.getStatus());
            }
            if(t instanceof QaGainAttendRecord){
                QaGainAttendRecord qaGainAttendRecord = (QaGainAttendRecord)t;
                if(DataUtil.idIsNotNull(qaGainAttendRecord.getReceiptorId())){
                    whereSql.append(" and receiptorId=").append(qaGainAttendRecord.getReceiptorId());
                }
                if(ObjectUtils.isNotEmpty(qaGainAttendRecord.getUserType())) {
                    whereSql.append(" and userType="+ qaGainAttendRecord.getUserType());
                }
            }
        }
         whereSql.append(" order by createTime DESC");
        return whereSql.toString();
    }
}
